<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="光缆名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入光缆名称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属支公司" prop="company">
            <el-select v-model="queryParams.company" placeholder="请选择所属支公司" clearable style="width:100%">
              <el-option
                v-for="dict in dict.type.sys_dept_dept_name"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="光缆长度" prop="length">
            <el-input v-model="queryParams.length" placeholder="请输入光缆长度" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="光缆起点炮筒" prop="startPoint">
            <el-input v-model="queryParams.startPoint" placeholder="请输入光缆起点炮筒" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="光缆终点炮筒" prop="endPoint">
            <el-input v-model="queryParams.endPoint" placeholder="请输入光缆终点炮筒" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding-top: 4px;">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- Rest of your template remains the same -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cables:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cables:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cables:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cables:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cablesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="光缆唯一标识" align="center" prop="id" />
      <el-table-column label="光缆名称" align="center" prop="name" />
      <el-table-column label="光缆起点炮筒" align="center" prop="startPoint" />
      <el-table-column label="光缆终点炮筒" align="center" prop="endPoint" />
      <el-table-column label="光缆长度" align="center" prop="length" />
      <el-table-column label="光缆类型" align="center" prop="type" />
      <el-table-column label="所属支公司" align="center" prop="company">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dept_dept_name" :value="scope.row.company"/>
        </template>
      </el-table-column>
      <el-table-column label="光缆状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cables:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cables:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改光缆表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="光缆名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入光缆名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属支公司" prop="company">
              <el-select v-model="form.company" placeholder="请选择所属支公司" clearable>
                <el-option
                  v-for="dict in dict.type.sys_dept_dept_name"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="光缆起点炮筒" prop="startPoint">
              <el-input
                v-model="form.startPoint"
                placeholder="请输入光缆起点炮筒"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="光缆终点炮筒" prop="endPoint">
              <el-input
                v-model="form.endPoint"
                placeholder="请输入光缆终点炮筒"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="光缆长度" prop="length">
              <el-input
                v-model="form.length"
                placeholder="请输入光缆长度"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20" justify="center">
          <el-col :span="12">
            <el-form-item>
              <el-button type="primary" icon="el-icon-check" size="mini" @click="submitForm">保存</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      <el-divider content-position="center">炮筒表信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddManholes">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteManholes">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="paginatedManholesList" :row-class-name="rowManholesIndex" @selection-change="handleManholesSelectionChange" ref="manholes">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="炮筒名称" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入炮筒名称" />
            </template>
          </el-table-column>
          <el-table-column label="炮筒经度" prop="longitude" width="130">
            <template slot-scope="scope">
              <el-input v-model="scope.row.longitude" placeholder="请输入炮筒经度" />
            </template>
          </el-table-column>
          <el-table-column label="炮筒纬度" prop="latitude" width="130">
            <template slot-scope="scope">
              <el-input v-model="scope.row.latitude" placeholder="请输入炮筒纬度" />
            </template>
          </el-table-column>
          <el-table-column label="炮筒类型" prop="type" width="120">
            <template slot-scope="scope">
              <el-select v-model="scope.row.type" placeholder="请选择炮筒类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="距离上一个炮筒的距离" prop="height" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.height" placeholder="请输入炮筒高度" />
            </template>
          </el-table-column>
          <el-table-column label="炮筒状态" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择炮筒状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="炮筒在光缆路径中的顺序" prop="sequence" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sequence" placeholder="请输入炮筒在光缆路径中的顺序" />
            </template>
          </el-table-column>
        </el-table>
      <pagination
        v-show="manholesTotal > 0"
        :total="manholesTotal"
        :page.sync="manholesPageNum"
        :limit.sync="manholesPageSize"
        @pagination="handleManholesPagination"
      />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCables, getCables, delCables, addCables, updateCables } from "@/api/system/cables";
import { listManholes, getManholes, delManholes, addManholes, updateManholes } from "@/api/system/manholes";

export default {
  name: "Cables",
  dicts: ['sys_dept_dept_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedManholes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 光缆表表格数据
      cablesList: [],
      // 炮筒表表格数据
      manholesList: [],
      // 炮筒表分页数据
      manholesPageNum: 1,
      manholesPageSize: 10,
      manholesTotal: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        startPoint: null,
        endPoint: null,
        length: null,
        type: null,
        company: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      cablesform:{
        id: null,
        length: null
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
  paginatedManholesList() {
      const start = (this.manholesPageNum - 1) * this.manholesPageSize;
      const end = start + this.manholesPageSize;
      return this.manholesList.slice(start, end);
    }
  },
  methods: {
    /** 查询光缆表列表 */
    getList() {
      this.loading = true;
      listCables(this.queryParams).then(response => {
        this.cablesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        startPoint: null,
        endPoint: null,
        length: null,
        type: null,
        company: null,
        status: null
      };
      this.manholesList = [];
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加光缆表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCables(id).then(response => {
        this.form = response.data;
        this.manholesList = response.data.manholesList;
        this.manholesTotal = response.data.manholesList.length;
        this.cablesform = response.data;
        this.cablesform.length = 0;
        if(this.cablesform.length == 0 ){
          const a = this.manholesList.forEach(element => {
          this.cablesform.length= Number(this.cablesform.length) + Number(element.height);
        });
        }

        this.cablesform.id = id;
        updateCables(this.cablesform).then(response => {
          console.log(response);
        });
        this.open = true;
        this.title = "修改光缆表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.manholesList = this.manholesList;
          if (this.form.id != null) {
            updateCables(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCables(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除光缆表编号为"' + ids + '"的数据项？').then(function() {
        return delCables(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 炮筒表序号 */
    rowManholesIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 炮筒表添加按钮操作 */
    handleAddManholes() {
      let obj = {};
      obj.name = "";
      obj.latitude = "";
      obj.longitude = "";
      obj.type = "";
      obj.height = "";
      obj.status = "";
      obj.sequence = "";
      this.manholesList.push(obj);
      this.manholesTotal = this.manholesList.length;
    },
    /** 炮筒表删除按钮操作 */
    handleDeleteManholes() {
      if (this.checkedManholes.length == 0) {
        this.$modal.msgError("请先选择要删除的炮筒表数据");
      } else {
        const manholesList = this.manholesList;
        const checkedManholes = this.checkedManholes;
        this.manholesList = manholesList.filter(function(item) {
          return checkedManholes.indexOf(item.index) == -1;
        });
        this.manholesTotal = this.manholesList.length;
      }
    },
    /** 复选框选中数据 */
    handleManholesSelectionChange(selection) {
      this.checkedManholes = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cables/export', {
        ...this.queryParams
      }, `cables_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.el-form-item {
  margin-bottom: 18px;
}
</style>